<template>
  <!-- 添加或修改岗位对话框 -->
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :before-close="cancel"
    width="500px"
    append-to-body
  >
    <FormConfigBox
      :config="{ rules, labelWidth: '100px' }"
      ref="formConfigBox"
      class="form"
      v-if="visible"
      :options="options"
      v-model="form"
    ></FormConfigBox>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { addBill, updateBill } from "@/api/bill/billList.js";
import dayjs from "dayjs";
export default {
  name: "AddAndEditBill",
  props: {
    success: {
      type: Function,
      default: () => {},
    },
  },
  data() {
    return {
      visible: false,
      title: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        spendingTime: {
          required: true,
          message: "请选择账单日期",
          trigger: "blur",
        },
        spendingPrice: {
          required: true,
          message: "请输入账单金额",
          trigger: "blur",
        },
      },
    };
  },

  mounted() {},

  methods: {
    open(type, data) {
      this.visible = true;
      if (type === "add") {
        this.title = "添加账单";
      } else {
        this.title = "修改账单";
      }
      this.form = data;
    },
    // 取消按钮
    cancel(success) {
      this.visible = false;
      this.$refs.formConfigBox.handleReset();
      if (success === true) {
        this.success();
      }
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.formConfigBox.handleSubmit((val) => {
        if (this.form.id != undefined) {
          updateBill(this.form).then((response) => {
            if (response.code === 200) {
              this.cancel(true);
              this.$modal.msgSuccess("修改成功");
            }
          });
        } else {
          addBill(val).then((response) => {
            if (response.code === 200) {
              this.cancel(true);
              this.$modal.msgSuccess("新增成功");
            }
          });
        }
      });
    },
  },
  computed: {
    options() {
      return [
        {
          tag: "date",
          prop: "spendingTime",
          label: "账单日期",
          value: this.form.id
            ? dayjs().format("YYYY-MM-DD")
            : this.form.spendingTime,
          attrs: {
            disabled: Boolean(this.form.id),
            format: "yyyy-MM-dd",
            "value-format": "yyyy-MM-dd",
          },
        },
        {
          tag: "number",
          prop: "spendingPrice",
          label: "账单金额",
          value: 0,
          attrs: {
            precision: 2,
            min: 0,
          },
        },
        {
          tag: "textarea",
          prop: "remark",
          label: "备注",
          value: "",
          attrs: {},
        },
      ];
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .form {
  .el-select,
  .el-date-editor,
  .el-input-number,
  .el-input-number--medium {
    width: 100%;
  }
}
</style>